package com.beyond.parse.binding.db;

import com.beyond.parse.binding.CommonBoundSql;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MySQLBoundSql implements CommonBoundSql {

    private static Map<String, String> columnTypeMap = new HashMap<String, String>();
    static  {
        // 字符型
        columnTypeMap.put("char", "String");
        columnTypeMap.put("varchar", "String");
        columnTypeMap.put("text", "String");

        // 数值型
        columnTypeMap.put("int", "Number");
        columnTypeMap.put("integer", "Number");
        columnTypeMap.put("float", "Number");
        columnTypeMap.put("double", "Number");

        // 时间型
        columnTypeMap.put("date", "Date");
        columnTypeMap.put("timestamp", "Date");
        columnTypeMap.put("datetime", "Date");
    }

    public List<String> boundSql(Map<String, String> map, List<String> list) {
        for (Map.Entry entry : map.entrySet()) {
            String params = "";

            String columnType = columnTypeMap.get(entry.getValue());
            if ("String".equals(columnType)) {
                params = "'test'";
            } else if ("Number".equals(columnType)) {
                params = "1";
            } else if ("Date".equals(columnType)) {
                params = "current_timestamp()";
            }

            for (int i = 0; i < list.size(); i ++) {
                String sql = list.get(i);
                sql = sql.replaceAll("'&" + entry.getKey() + "&'", params);
                list.set(i, sql);
            }
        }

        return list;
    }
}
